package me.rvj.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: JavaWeb
 * @description: 检测用户Cookie，实现强制登录功能
 * @author: Rv_Jiang
 * @date: 2021/7/6 20:34
 */
@WebServlet(name = "CheckUserCookieServlet",value = "/CheckUserCookieServlet")
public class CheckUserCookieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        Cookie[] cookies = req.getCookies();
        if(cookies==null){
            resp.getWriter().print(false);
            System.out.println("null");
            return;
        }else if(cookies.length==0){
            resp.getWriter().print(false);
            return;
        }
        System.out.println(cookies.length);
        for (Cookie item : cookies) {
            System.out.println(item.getName()+" : "+item.getValue());
            if("userName".equals(item.getName())){
                resp.getWriter().print(true);
                return;
            }
        }
        resp.getWriter().print(false);
    }
}
